package com.wxtt.android.view;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * 说明：下拉刷新组件
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/2/16 15:03
 * <p/>
 * 版本：verson 1.0
 */
public class SwipeRefresh extends SwipeRefreshLayout implements SwipeRefreshLayout.OnRefreshListener {

    //下拉正在刷新
    public final static int STATE_REFRESH = 0;
    //刷新完成
    public final static int STATE_NONE = 1;
    //下拉刷新没有更多数据
    public final static int STATE_REFRESH_NOMORE = 2;

    //刷新监听器
    private RefreshListener mListener;
    //刷新状态
    private int mState = 0;

    public SwipeRefresh(Context context) {
        super(context);
        init();
    }

    public SwipeRefresh(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 说明：初始化
     */
    private void init(){
        //设置刷新头颜色
        setRefreshColor();
        //设置刷新监听
        setOnRefreshListener(this);
        //设置暂无刷新状态
        setState(STATE_NONE);
    }

    /**
     * 说明：设置刷新监听器
     * @param listener
     */
    public void setRefreshListener(RefreshListener listener){
        mListener = listener;
    }

    /**
     * 说明：自动刷新
     */
    public void autoRefresh(){
        final View view;
        if (getChildCount() > 0){
            view = getChildAt(0);
        }else {
            view = null;
        }
        if (view == null){
            return;
        }
        if (view.getHeight()== 0){
            view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    setRefreshing(true);
                    onRefresh();
                }
            });
        }else {
            setRefreshing(true);
            onRefresh();
        }
    }

    /**
     * 说明：设置刷新状态
     * @param state
     */
    public void setState(int state){
        switch (state){
            case STATE_REFRESH:
                mState = STATE_REFRESH;
                break;
            case STATE_NONE:
                mState = STATE_NONE;
                break;
            case STATE_REFRESH_NOMORE:
                mState = STATE_REFRESH_NOMORE;
                break;
        }
    }

    /**
     * 说明：获取刷新状态
     * @return
     */
    public int getState(){
        return mState;
    }

    /**
     * 说明：设置刷新头颜色
     */
    public void setRefreshColor(int... colors){
        if (colors == null || colors.length == 0){
            int color1 = Color.parseColor("#ff00ddff");
            int color2 = Color.parseColor("#ffffbb33");
            int color3 = Color.parseColor("#ffff4444");
            int color4 = Color.parseColor("#ffaa66cc");
            setColorSchemeColors(color1,color2,color3,color4);
        }else {
            setColorSchemeColors(colors);
        }
    }

    /**
     * 说明：设置顶部正在加载的状态
     */
    public void setRefreshLoadingState(){
        //正在刷新
        setRefreshing(true);
        //防止多次刷新
        setEnabled(false);
    }

    /**
     * 说明：设置顶部加载完毕的状态
     */
    public void setRefreshLoadedState(){
        //刷新完毕
        setRefreshing(false);
        setEnabled(true);
        mState = STATE_NONE;
    }

    @Override
    public void onRefresh() {
        if (getState() == STATE_REFRESH){
            return;
        }
        setRefreshLoadingState();
        mState = STATE_REFRESH;
        if (mListener != null){
            mListener.onRefresh();
        }else {
            setRefreshLoadedState();
        }
    }

    /**
     * 说明：刷新监听
     */
    public interface RefreshListener{
        //下拉刷新更多
        void onRefresh();
    }
}

